package fire.and.near.challenger.fragments;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import fire.and.near.challenger.ChallengeDetailsActivity;
import fire.and.near.challenger.R;
import fire.and.near.challenger.database.ChallengeModel;
import fire.and.near.challenger.database.TableConstants.ChallengesTable;
import fire.and.near.challenger.database.TableConstants.FriendsTable;

public class ChallengesHistoryFragment extends Fragment {

	private List<ChallengeModel> challenges = null;
	private ListView myChallengesList = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.challenges_history_fragment,
				container, false);

		if (this.challenges != null) {
			setChallengesListAdapter(v);
		}

		return v;
	}

	public List<ChallengeModel> getChallenges() {
		return challenges;
	}

	public void setChallenges(List<ChallengeModel> challenges) {
		this.challenges = challenges;
	}

	public void setChallengesAndAdapter(List<ChallengeModel> challenges) {
		this.challenges = challenges;
		setChallengesListAdapter(this.getView());
	}

	private void setChallengesListAdapter(View v) {
		myChallengesList = (ListView) v
				.findViewById(R.id.listView_challenges);
		ChallengesListAdapter adapter = new ChallengesListAdapter(
				getActivity(), R.id.listView_challenges, this.challenges);
		myChallengesList.setAdapter(adapter);
		adapter.notifyDataSetChanged();

		myChallengesList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(getActivity(),
						ChallengeDetailsActivity.class);
				Bundle extras = new Bundle();

				extras.putInt(ChallengesTable.ID, challenges.get(position)
						.getID());
				extras.putString(ChallengesTable.DESCRIPTION,
						challenges.get(position).getDescription());
				extras.putString(ChallengesTable.ANSWER, 
						challenges.get(position).getAnswer());
				extras.putString(ChallengesTable.HASHTAG,
						challenges.get(position).getHashtag());
				extras.putString(ChallengesTable.PHOTO_PATH,
						challenges.get(position).getPhotoPath());
				extras.putLong(ChallengesTable.CHALLENGER_ID,
						challenges.get(position).getChallenger().getFbID());
				extras.putLong(ChallengesTable.CHALLENGED_ID,
						challenges.get(position).getChallengedID());
				extras.putString(FriendsTable.NAME, challenges.get(position)
						.getChallenger().getName());
				extras.putBoolean(ChallengesTable.ACCEPTED, challenges.get(position)
						.isAccepted());
				i.putExtras(extras);

				startActivity(i);
			}
		});
	}
}
